<?php
/**
 * Created by PhpStorm.
 * User: xianxuefeng
 * Date: 2018/7/3
 * Time: 17:46
 */
namespace app\controllers\marketing;

use Yii;
use GuzzleHttp\Client;
use yii\web\Controller;
use yii\data\Pagination;
class AnomalyMonitoringController extends \app\base\Controller
{

    /*
     *
     * 获取异常监控列表
     * author xianxuefeng
     * date 2018-7-3
     * params [
     *     'start_time' =>'开始时间',
     *     'end_time' => '结束时间',
     *     'channel' => '发送渠道',
     *     'title' => '标题，监控项',
     *     'page' => '初始页面为1',
     * ]
     *
     */
    public function actionExceptionRecordList()
    {
        $page = 0;
        $params = $_REQUEST;
        if (! empty($params['page'])) {
            $page = $params['page'] - 1;
        }
        $limit = 20;
        if (!empty($params['pagesize'])) {
            $limit = $params['pagesize'];
        }
        $channelArr = [0 => '全部',2 => 'emarsys',5 => 'benchmark', 9 => 'webpower'];
//        $monitorArr = [1 => '下载文件失败',2=>'解压失败',3=>'数据更新异常' ];
        $monitorArr = [0 => '全部',1 => '下载文件失败',2=>'解压文件失败',3=>'数据更新异常' ];
        $defChannel = 0;
        $defMonitor = 0;
        $where = '';
        if ( isset($params['channel']) &&  ! empty($params['channel']) && '全部' != $params['channel']) {
            $where .= " channel = '{$params['channel']}' AND ";
            $defChannel = array_search($params['channel'],$channelArr);
        }
        if (isset($params['monitor_id']) && !empty($params['monitor_id']) ) {
            $where .= " monitor_id = {$params['monitor_id']} AND ";
//            $defMonitor = array_search($params['monitor_type'],$monitorArr);
            $defMonitor = $params['monitor_id'];
        }
//        $yesterday = strtotime('-1 day');
        $lastmonth = strtotime('-1 month');
        $today = time();
        $timelist = [date('Y-m-d',$lastmonth),date('Y-m-d',$today)];
        if ( isset($params['reservation_time']) && ! empty($params['reservation_time'])  ) {
            $timelist = explode(' - ',$params['reservation_time']);
            $start_time = strtotime($timelist[0]);
            $end_time = strtotime($timelist[1]) + 24*60*60;
            $where .= " create_time between {$start_time} and {$end_time} ";
        }
        if ( ! empty($where)) {
            $where = " where ".$where;
        }
        //分页
        $totalCountSql = " SELECT COUNT(id) AS total FROM exception_record " . $where ;
        $totalCount = Yii::$app->db->createCommand($totalCountSql)->queryScalar();
//        echo $totalCountSql;

        //echo $totalCountSql;die;

        $offset = $page * $limit;
        $pages = new Pagination(['totalCount' =>$totalCount, 'pageSize' => $limit, 'defaultPageSize' => $limit]);
        //模板数据
        $limit = " LIMIT {$offset}, {$limit}";
        $order = " ORDER BY id DESC ";
        $sql = " SELECT id,title,channel,explain_info, exception_info,monitor,FROM_UNIXTIME(create_time, '%Y-%c-%d %h:%i:%s' ) as create_time FROM exception_record " . $where . $order . $limit;
//       echo $sql;die;
        $data = Yii::$app->db->createCommand($sql)->queryAll();
//
//        var_dump($data);die;
        $formData = [
            "startTime" => $timelist[0],
            "endTime" => $timelist[1],
            "page"    => 1,
            "pageSize" => 20,
            "channelArr" => $channelArr,
            "defChannel" =>$defChannel ,
            "monitorArr" => $monitorArr,
            "defMonitor" => $defMonitor,
        ];
//        var_dump($formData);die;
        return $this->render("//marketing/anomaly-monitoring/exception-record-list",
            ["data" => $data,"totalCount" => $totalCount,"formData" => $formData,'pages' =>$pages, "site" => "marketing_email"]);
    }

    /**
     * 获取日常日志详情
     *
     */
    public function actionExceptionRecordDetail() {
        $params = $_REQUEST;
        $where = '';
        if (! empty($params['id'])) {
          $where = "where id = {$params['id']} ";
        }
        if (empty($where)) {
            $this->sysMsg('警告', '数据不完整，缺少id!',
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        $sql = " SELECT id,title,channel,explain_info, exception_info,monitor,FROM_UNIXTIME(create_time, '%Y-%c-%d %h:%i:%s' ) as create_time FROM exception_record " . $where ;
        $data = Yii::$app->db->createCommand($sql)->queryAll();
        if (empty($data)) {
            $this->sysMsg('警告', '没有更多信息',
                [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
        }
        return $this->render("//marketing/anomaly-monitoring/exception-record-detail", ["data" => $data[0]]);
    }

}